/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rultax.blackice.example;

import com.rultax.blackice.example.Boundary;
import com.rultax.blackice.example.Car;
import com.rultax.blackice.Physics;
import com.rultax.blackice.ProcessingObject;
import java.util.ArrayList;
import java.util.List;
import processing.core.PApplet;

/**
 *
 * @author Scott
 */
public class Boxes extends PApplet {

    private Physics physics;
    private List<ProcessingObject> processingObjects;
    private Car car;
    private boolean[] keys;

    public static void main(String args[]) {
	PApplet.main(new String[]{"com.rultax.blackice.example.Boxes"});
    }

    @Override
    public void setup() {
	size(600, 300);
	smooth();

	keys = new boolean[4];
	for (int i = 0; i < keys.length; i++) {
	    keys[i] = false;
	}
	
	physics = new Physics(this);
	physics.createWorld();
	physics.setGravity(0, 0);

	processingObjects = new ArrayList<ProcessingObject>();

	processingObjects.add(new Boundary(width / 2, 5, width, 10, this, physics));
	processingObjects.add(new Boundary(5, height / 2, 10, height, this, physics));
	processingObjects.add(new Boundary(width / 2, height - 5, width, 10, this, physics));
	processingObjects.add(new Boundary(width - 5, height / 2, 10, height, this, physics));
	processingObjects.add(new Boundary(width / 2, height * 0.75f, width * 0.5f, 10, this, physics));
	processingObjects.add(new Boundary(width * 0.25f + 5, height * 0.5f, 10, height * 0.5f, this, physics));
	processingObjects.add(new Boundary(width * 0.75f - 5, height * 0.5f, 10, height * 0.5f, this, physics));
	processingObjects.add(new Boundary(width / 2, height * 0.25f, 10, height * 0.5f, this, physics));
	car = new Car(this, physics);
    }

    @Override
    public void draw() {
	background(255);

	physics.step();

	for (ProcessingObject processingObject : processingObjects) {
	    processingObject.display();
	}
	
	car.display();
	
	if(keys[ACCELERATE])
	    car.accelerate();
	else if(keys[DECCELARATE])
	    car.brake();
	else 
	    car.coast();
	    
	if(keys[STEER_LEFT])
	    car.left();
	else if(keys[STEER_RIGHT])
	    car.right();
	else if(!keys[STEER_RIGHT] && !keys[STEER_LEFT])
	    car.straight();
		
	car.adjust(1.0f / 60f);
    }
    
    @Override
    public void keyPressed(){
	if(keyCode == PApplet.UP)
	    keys[ACCELERATE] = true;
	else if(keyCode == PApplet.DOWN)
	    keys[DECCELARATE] = true;
	else if(keyCode == PApplet.LEFT)
	    keys[STEER_LEFT] = true;
	else if(keyCode == PApplet.RIGHT)
	    keys[STEER_RIGHT] = true;
}
    
    @Override
    public void keyReleased(){
	if(keyCode == PApplet.UP)
	    keys[ACCELERATE] = false;
	else if(keyCode == PApplet.DOWN)
	    keys[DECCELARATE] = false;
	else if(keyCode == PApplet.LEFT)
	    keys[STEER_LEFT] = false;
	else if(keyCode == PApplet.RIGHT)
	    keys[STEER_RIGHT] = false;
    }
    
    private static final int ACCELERATE = 0;
    private static final int DECCELARATE = 1;
    private static final int STEER_LEFT = 2;
    private static final int STEER_RIGHT = 3;
}
